package com.yang.studentsapp.model;

import android.util.Log;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.chat.adapter.EMACallManager;
import com.hyphenate.chat.adapter.EMAContactManager;
import com.hyphenate.exceptions.HyphenateException;
import com.yang.studentsapp.R;
import com.yang.studentsapp.utils.ResUtils;
import com.yang.studentsapp.utils.WorkFactory;

/**
 * Created by ${Ян-Вэнь-Чао} on 2016/10/17.
 */
public class ModeAccount extends ModeBase {
    /**
     * 注册帐号
     * @param name 帐号名
     * @param pwd 密码
     * @param register
     */
    public void register(final String name, final String pwd,
                         final IActionRegister register
            , final IActionSuccess<String> actionSuccess) {
        WorkFactory.instance().service().submit(new Runnable() {
            @Override
            public void run() {
                try {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        register.start(R.string.onRegister);
                    }
                });
                    EMClient.getInstance().createAccount(name, pwd);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            actionSuccess.succes("注册成功！！！");
                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    public void login(final String name,
                      final String pwd,
                      final IActionLogin actionLogin) {
        WorkFactory.instance().service().submit(new Runnable() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        actionLogin.start(ResUtils.getString(R.string.Is_landing));

                    }
                });
                EMClient.getInstance().login(name, pwd, new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                actionLogin.succes(ResUtils.getString(R.string.login_ok));
                            }
                        });
                    }

                    @Override
                    public void onError(int i, final String s) {
                        Log.e("tag", s);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                           actionLogin.error(s);
                            }
                        });
                    }

                    @Override
                    public void onProgress(int i, String s) {

                    }
                });
            }
        });

    }

}
